var fs = require('fs');
var fileUtil = require('njserver').fileUtil;

module.exports = {
    get: {
        download: function (req, res) {
            res.setHeader("Content-Disposition", "attachment;filename=" + encodeURIComponent(fileUtil.getFileInfo(req.query.path).base));
            res.contentType = "application/octet-stream";
            res.sendFile(req.query.path, false);
        },

        base64: function (req, res) {
            fs.readFile(req.query.path, (err, data) => {
                if (!err) {
                    res.send('data:' + sysConfig.mime[fileUtil.getFileExt(req.query.path)] + ';base64,' + data.toString('base64'));
                } else {
                    res.send({
                        code: 201,
                        message: "base64编码失败",
                        fileName: req.query.path
                    });
                }
            });
        },

        view: function (req, res) {
            res.sendFile(req.query.path, true);
        }
    },

    post: {
        mkdir: function (req, res) {
            fileUtil.mkdirs(req.form.path, function (err) {
                if (!err) {
                    res.send({
                        code: 200,
                        message: "创建成功",
                        fileName: req.form.path
                    });
                } else {
                    res.send({
                        code: 201,
                        message: "创建失败",
                        fileName: req.form.path
                    });
                }
            });
        },

        deldir: function (req, res) {
            fileUtil.deldirs(req.form.path);
            res.send({
                code: 200,
                message: "删除成功",
                fileName: req.form.path
            });
        },

        add: function (req, res) {
            var uploadDir = req.form.path;
            fileUtil.mkdirs(uploadDir, function (err) {
                if (err) {
                    res.send({
                        code: 201,
                        message: "上传失败",
                        fileName: ''
                    });
                    return;
                }

                var file = req.files.file;
                if (file) {
                    fs.exists('./' + sysConfig.uploadTemp + '/' + req.form.name, function (exists) {
                        if (exists && req.form.chunks) {
                            var readerStream = fs.createReadStream(file.path);
                            var writerStream = fs.createWriteStream('./' + sysConfig.uploadTemp + '/' + req.form.name, {
                                flags: 'a'
                            });
                            readerStream.pipe(writerStream).on('close', function () {
                                fs.unlink(file.path, function () {});
                                if (req.form.chunk == parseInt(req.form.chunks) - 1) {
                                    fs.rename('./' + sysConfig.uploadTemp + '/' + req.form.name, './' + uploadDir + '/' + req.form.filename, function (err) {
                                        if (!err) {
                                            res.send({
                                                code: 200,
                                                message: "上传成功",
                                                fileName: req.form.filename
                                            });
                                        } else {
                                            res.send({
                                                code: 201,
                                                message: "上传失败",
                                                fileName: req.form.filename
                                            });
                                        }
                                    });
                                } else {
                                    res.send({
                                        code: 200,
                                        message: "上传成功",
                                        fileName: req.form.name
                                    });
                                }
                            });
                        } else {
                            fs.rename(file.path, './' + (req.form.chunks ? req.form.chunks == 1 ? uploadDir + '/' + req.form.filename : sysConfig.uploadTemp + '/' + req.form.name : uploadDir + '/' + file.name), function (err) {
                                fs.unlink(file.path, function () {});
                                if (!err) {
                                    res.send({
                                        code: 200,
                                        message: "上传成功",
                                        fileName: req.form.chunks ? req.form.chunks == 1 ? req.form.filename : req.form.name : file.name
                                    });
                                } else {
                                    res.send({
                                        code: 201,
                                        message: "上传失败",
                                        fileName: filename
                                    });
                                }
                            });
                        }
                    });

                    return;
                }

                res.send({
                    code: 201,
                    message: "上传失败",
                    fileName: ''
                });
            });
        },

        delete: function (req, res) {
            fs.unlink(req.form.path, function (err) {
                if (!err) {
                    res.send({
                        code: 200,
                        message: "删除成功",
                        fileName: req.form.path
                    });
                } else {
                    res.send({
                        code: 201,
                        message: "删除失败",
                        fileName: req.form.path
                    });
                }
            });
        },

        rename: function (req, res) {
            fs.rename(req.form.path, req.form.newPath, function (err) {
                if (!err) {
                    res.send({
                        code: 200,
                        message: "重命名成功",
                        fileName: req.form.path
                    });
                } else {
                    res.send({
                        code: 201,
                        message: "重命名失败",
                        fileName: req.form.path
                    });
                }
            });
        },

        exists: function (req, res) {
            fs.exists(req.form.path, function (exists) {
                if (exists) {
                    res.send({
                        code: 200,
                        message: "已存在",
                        fileName: req.form.path
                    });
                } else {
                    res.send({
                        code: 201,
                        message: "不存在",
                        fileName: req.form.path
                    });
                }
            });
        }
    }
};